﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FimetContoDeposito.BLL.Objects;
using FimetContoDeposito.DAL;
using GrayParrot.Core;

namespace FimetContoDeposito.BLL.Facades
{
    public class ArticoliPerFornitoreFacade : FacadeBase<ArticoliPerFornitoreFacade>
    {
        FCDContext m_ctx = null;

        public Repository<FCD_ArticoliPerFornitori, Guid> Repo { get; internal set; }


        public ArticoliPerFornitoreFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new FCDContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<FCD_ArticoliPerFornitori, Guid>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(FCD_ArticoliPerFornitori item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(FCD_ArticoliPerFornitori item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(FCD_ArticoliPerFornitori item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_ArticoliPerFornitori> Get(Guid id)
        {
            ReturnValue<FCD_ArticoliPerFornitori> retValue = new ReturnValue<FCD_ArticoliPerFornitori>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<FCD_ArticoliPerFornitori> Get(Guid idArticolo, Guid idFornitore)
        {
            ReturnValue<FCD_ArticoliPerFornitori> retValue = new ReturnValue<FCD_ArticoliPerFornitori>();

            try
            {
                var objs = from x in m_ctx.FCD_ArticoliPerFornitoris
                           where x.IdArticolo == idArticolo && x.IdFornitore == idFornitore
                           select x;

                if (objs.Any())
                {
                    retValue.Value = objs.FirstOrDefault();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(Guid idArticolo, Guid idFornitore)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();
            retValue.HasValue = true;
            retValue.Value = false;

            try
            {
                ReturnValue<FCD_ArticoliPerFornitori> obj = this.Get(idArticolo, idFornitore);

                if (obj.HasValue)
                {
                    if (obj.Value != null)
                    {
                        ReturnValue<bool> ret = this.Delete(obj.Value);
                        retValue = ret;
                    }
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FCD_ArticoliPerFornitori>> GetAll()
        {
            ReturnValue<IQueryable<FCD_ArticoliPerFornitori>> retValue = new ReturnValue<IQueryable<FCD_ArticoliPerFornitori>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<NameValue<string, Guid>>> GetAll(Guid idFornitore)
        {
            ReturnValue<IQueryable<NameValue<string, Guid>>> retValue = new ReturnValue<IQueryable<NameValue<string, Guid>>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = from x in m_ctx.FCD_AnagraficaArticolis
                           join y in m_ctx.FCD_ArticoliPerFornitoris on x.Id equals y.IdArticolo
                           where y.IdFornitore == idFornitore
                           select new NameValue<string, Guid>() {
                               name = x.Descrizione,
                               value = x.Id
                           };

                if (objs.Any())
                {
                    retValue.Value = objs;
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<NameValue<string, Guid>>> GetAllCodice(Guid idFornitore)
        {
            ReturnValue<IQueryable<NameValue<string, Guid>>> retValue = new ReturnValue<IQueryable<NameValue<string, Guid>>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = from x in m_ctx.FCD_AnagraficaArticolis
                           join y in m_ctx.FCD_ArticoliPerFornitoris on x.Id equals y.IdArticolo
                           where y.IdFornitore == idFornitore
                           select new NameValue<string, Guid>()
                           {
                               name = x.Codice,
                               value = x.Id
                           };

                if (objs.Any())
                {
                    retValue.Value = objs;
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<FornitoreArticoloBag>> GetFornitoriPerArticoloBag(Guid id)
        {
            ReturnValue<IQueryable<FornitoreArticoloBag>> retValue = new ReturnValue<IQueryable<FornitoreArticoloBag>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = from x in m_ctx.FCD_VwFornitoriPerArticoloes
                           where x.IdArticolo == id
                           select new FornitoreArticoloBag()
                           {
                               PrezzoUnitario = x.PrezzoUnitario.Value,
                               ScontoFornitore = x.ScontoFornitore.Value,
                               ScortaMinima = x.ScortaMinima.Value,
                               Fornitore = x.IdFornitore,
                               DescrFornitore = x.Descrizione
                           };

                if (objs.Any())
                {
                    retValue.Value = objs;
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }
    }
}
